<?php

/**
 * This is the model class for table "user_view".
 *
 * The followings are the available columns in table 'user_view':
 * @property integer $user_id
 * @property integer $user_id_viewed
 */
class mUserView extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return mUserView the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_view';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, user_id_viewed', 'required'),
			array('user_id, user_id_viewed', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_id_viewed', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_id_viewed' => 'User Id Viewed',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_id_viewed',$this->user_id_viewed);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function getUserViewed($user_viewed){
      $user = Yii::app()->db->createCommand()
                  ->select('uv.*, up.user_id as user_idup, up.link_img, p.gender, p.birthdate, uf.user_id as favorite')
                  ->from('user_view uv')                  
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = uv.user_id')
                  ->leftjoin('profiles p', 'p.user_id = uv.user_id')
                  ->leftjoin('user_favorite uf', 'uf.user_id=:user_id and uf.user_favorited=uv.user_id', array(':user_id'=>$user_viewed))
                  ->where('uv.user_id_viewed=:user_id_viewed', array(':user_id_viewed'=>$user_viewed))
                  ->limit(3)
                  ->queryAll();
    if(count($user)>0)
      return $user;
    else
      return false;
    }
    
    public function getUserViewWithPagination($user_id, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*, uv.date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
                  ->from('user_view uv')
                  ->leftjoin('profiles p', 'p.user_id=uv.user_id_viewed')
                  ->leftjoin('users u', 'u.user_id=uv.user_id_viewed')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_id_viewed')
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->leftjoin('user_favorite uf', 'uf.user_favorited = uv.user_id_viewed and uf.user_id=:user_id', array(':user_id'=>user()->getUserProperty('user_id')))
                  ->where('uv.user_id=:user_id',array(':user_id'=>$user_id))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
    
    public function getUserViewedWithPagination($user_id_viewed, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*, uv.date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
                  ->from('user_view uv')
                  ->leftjoin('profiles p', 'p.user_id=uv.user_id')
                  ->leftjoin('users u', 'u.user_id=uv.user_id')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_id')
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->leftjoin('user_favorite uf', 'uf.user_favorited = uv.user_id and uf.user_id=:user_id', array(':user_id'=>user()->getUserProperty('user_id')))
                  ->where('uv.user_id_viewed=:user_id_viewed',array(':user_id_viewed'=>$user_id_viewed))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
}